package _case

import "fmt"

type ToString interface {
	String() string
}

func (u User) String() string {
	return fmt.Sprintf("ID:%d,Name:%s,Age:%d", u.ID, u.Name, u.Age)
}

func (addr Address) String() string {
	return fmt.Sprintf("ID:%d,Province:%s,City:%s", addr.ID, addr.Province, addr.City)
}

type GetKey[T comparable] interface {
	any
	Get() T
}

func (u User) Get() int {
	return u.ID
}

func (addr Address) Get() int64 {
	return addr.ID
}

// 列表转集合
func ListToMap[K comparable, T GetKey[K]](list ListT[T]) MapT[K, T] {
	m := make(MapT[K, T])

	for _, v := range list {
		m[v.Get()] = v

	}

	return m
}

func InterfaceCase() {
	uList := []GetKey[int]{
		User{1, "jc", 18},
		User{2, "Tom", 20},
	}
	fmt.Println(ListToMap[int, GetKey[int]](uList))

	addrList := []GetKey[int64]{
		Address{1, "广东", "深圳"},
		Address{2, "广西", "碧海"},
	}
	fmt.Println(ListToMap[int64, GetKey[int64]](addrList))
}
